package com.liuyjy.commons.masking.masking;


import cn.hutool.core.util.StrUtil;
import com.liuyjy.utils.utils.AESUtil;
import com.liuyjy.utils.utils.EmptyUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author liuyjy
 * @Date 2022/7/18 20:41
 * @Description: 脱敏验证
 **/
public enum DataMaskingFunc {
    /**
     * 不敏
     */
    NO_MASK((str, maskChar) -> str),
    /**
     * 加密
     */
    ENCRYPT((str, maskChar) -> AESUtil.encrypt(str.toString())),
    /**
     * 解密
     */
    DECRYPT((str, maskChar) -> AESUtil.decrypt(str)),

    /**
     * 手机号脱敏
     */
    MOBILE((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }),
    /**
     * 身份证脱敏
     */
    IDCARD((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", DataMaskingOperation.MASK_CHAR);
    }),
    /**
     * 身份证脱敏
     */
    IDCARD1((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(?<=\\w{1})\\w(?=\\w{1})", DataMaskingOperation.MASK_CHAR);
    }),
    /**
     * 邮箱脱敏
     */
    EMAIL((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(\\w+)\\w{2,}@(\\w+)", "$1****@$2");
    }),
    /**
     * 银行卡号脱敏
     */
    BANK_CARD((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2");
    }),
    /**
     * 地址脱敏
     */

    ADDRESS((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return strUtilHide(string, 3);
    }),
    /**
     * 密码脱敏
     */
    PASSWORD((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(\\w{3})\\w{6,}(\\w{3})", "$1****$2");
    }),
    /**
     * 账号脱敏
     */
    ACCOUNT((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        return string.replaceAll("(\\w{3})\\w{6,}(\\w{3})", "$1****$2");
    }),

    /**
     * 中文姓名脱敏
     */
    CHINESE_NAME((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        String serializeValue = "";
        if (string.length() < 3) {
            serializeValue = string.replaceAll(".*(?=[\\u4e00-\\u9fa5])", DataMaskingOperation.MASK_CHAR);
        } else {
            serializeValue = string.replaceAll("(?<=[\\u4e00-\\u9fa5]).*(?=[\\u4e00-\\u9fa5])", DataMaskingOperation.MASK_CHAR);
        }
        return serializeValue;
    }),

    /**
     * 全敏
     */
    ALL_MASK((str, maskChar) -> {
        if (EmptyUtil.isEmpty(str)) {
            return str;
        }
        String string = str.toString();
        //防止二次脱敏
        if(string.contains(DataMaskingOperation.MASK_CHAR)){
            return str;
        }
        if (StringUtils.isNotBlank(string)) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < string.length(); i++) {
                sb.append(StringUtils.isNotBlank(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
            }
            return sb.toString();
        } else {
            return string;
        }
    });

    private final DataMaskingOperation operation;

    private DataMaskingFunc(DataMaskingOperation operation) {
        this.operation = operation;
    }

    public DataMaskingOperation operation() {
        return this.operation;
    }

    /**
     * start从0开始
     */
    public static Object strUtilHide(String obj, int start, int end) {
        return StrUtil.hide(obj, start, end);
    }

    public static Object strUtilHide(Object obj, int start) {
        return strUtilHide(((String) obj), start, ((String) obj).length());
    }
}
